﻿
namespace ATA.Windows.Forms
{
	/// <summary>
	/// abstract NhomSanPham typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class NhomSanPhamDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<NhomSanPhamDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.NhomSanPham _currentNhomSanPham = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxNhomSanPhamDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxNhomSanPhamErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxNhomSanPhamBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the NhomSanPhamId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNhomSanPhamIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the SanPhamId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxSanPhamIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the LastUpdate property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLastUpdateDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the UpdateBy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxUpdateByDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.NhomSanPham> _NhomSanPhamList;
				
		/// <summary> 
		/// The list of NhomSanPham to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.NhomSanPham> NhomSanPhamList
		{
			get {return this._NhomSanPhamList;}
			set
			{
				this._NhomSanPhamList = value;
				this.uxNhomSanPhamBindingSource.DataSource = null;
				this.uxNhomSanPhamBindingSource.DataSource = value;
				this.uxNhomSanPhamDataGridView.DataSource = null;
				this.uxNhomSanPhamDataGridView.DataSource = this.uxNhomSanPhamBindingSource;				
				//this.uxNhomSanPhamBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxNhomSanPhamBindingSource_ListChanged);
				this.uxNhomSanPhamBindingSource.CurrentItemChanged += new System.EventHandler(OnNhomSanPhamBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnNhomSanPhamBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentNhomSanPham = uxNhomSanPhamBindingSource.Current as Entities.NhomSanPham;
			
			if (_currentNhomSanPham != null)
			{
				_currentNhomSanPham.Validate();
			}
			//_NhomSanPham.Validate();
			OnCurrentEntityChanged();
		}

		//void uxNhomSanPhamBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.NhomSanPham"/> instance.
		/// </summary>
		public Entities.NhomSanPham SelectedNhomSanPham
		{
			get {return this._currentNhomSanPham;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxNhomSanPhamDataGridView.VirtualMode;}
			set
			{
				this.uxNhomSanPhamDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxNhomSanPhamDataGridView.AllowUserToAddRows;}
			set {this.uxNhomSanPhamDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxNhomSanPhamDataGridView.AllowUserToDeleteRows;}
			set {this.uxNhomSanPhamDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxNhomSanPhamDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxNhomSanPhamDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="NhomSanPhamDataGridViewBase"/> class.
		/// </summary>
		public NhomSanPhamDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxNhomSanPhamDataGridView = new System.Windows.Forms.DataGridView();
			this.uxNhomSanPhamBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxNhomSanPhamErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxNhomSanPhamIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxSanPhamIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxLastUpdateDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxUpdateByDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxNhomSanPhamDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNhomSanPhamBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNhomSanPhamErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxNhomSanPhamErrorProvider
			// 
			this.uxNhomSanPhamErrorProvider.ContainerControl = this;
			this.uxNhomSanPhamErrorProvider.DataSource = this.uxNhomSanPhamBindingSource;						
			// 
			// uxNhomSanPhamDataGridView
			// 
			this.uxNhomSanPhamDataGridView.AutoGenerateColumns = false;
			this.uxNhomSanPhamDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxNhomSanPhamDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxNhomSanPhamIdDataGridViewColumn,
		this.uxSanPhamIdDataGridViewColumn,
		this.uxLastUpdateDataGridViewColumn,
		this.uxUpdateByDataGridViewColumn			});
			this.uxNhomSanPhamDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxNhomSanPhamDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxNhomSanPhamDataGridView.Name = "uxNhomSanPhamDataGridView";
			this.uxNhomSanPhamDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxNhomSanPhamDataGridView.TabIndex = 0;	
			this.uxNhomSanPhamDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxNhomSanPhamDataGridView.EnableHeadersVisualStyles = false;
			this.uxNhomSanPhamDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnNhomSanPhamDataGridViewDataError);
			this.uxNhomSanPhamDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNhomSanPhamDataGridViewCellValueNeeded);
			this.uxNhomSanPhamDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNhomSanPhamDataGridViewCellValuePushed);
			
			//
			// uxNhomSanPhamIdDataGridViewColumn
			//
			this.uxNhomSanPhamIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNhomSanPhamIdDataGridViewColumn.DataPropertyName = "NhomSanPhamId";
			this.uxNhomSanPhamIdDataGridViewColumn.HeaderText = "NhomSanPhamId";
			this.uxNhomSanPhamIdDataGridViewColumn.Name = "uxNhomSanPhamIdDataGridViewColumn";
			this.uxNhomSanPhamIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNhomSanPhamIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNhomSanPhamIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxSanPhamIdDataGridViewColumn
			//
			this.uxSanPhamIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSanPhamIdDataGridViewColumn.DataPropertyName = "SanPhamId";
			this.uxSanPhamIdDataGridViewColumn.HeaderText = "SanPhamId";
			this.uxSanPhamIdDataGridViewColumn.Name = "uxSanPhamIdDataGridViewColumn";
			this.uxSanPhamIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSanPhamIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSanPhamIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxLastUpdateDataGridViewColumn
			//
			this.uxLastUpdateDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLastUpdateDataGridViewColumn.DataPropertyName = "LastUpdate";
			this.uxLastUpdateDataGridViewColumn.HeaderText = "LastUpdate";
			this.uxLastUpdateDataGridViewColumn.Name = "uxLastUpdateDataGridViewColumn";
			this.uxLastUpdateDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLastUpdateDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLastUpdateDataGridViewColumn.ReadOnly = false;		
			//
			// uxUpdateByDataGridViewColumn
			//
			this.uxUpdateByDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxUpdateByDataGridViewColumn.DataPropertyName = "UpdateBy";
			this.uxUpdateByDataGridViewColumn.HeaderText = "UpdateBy";
			this.uxUpdateByDataGridViewColumn.Name = "uxUpdateByDataGridViewColumn";
			this.uxUpdateByDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxUpdateByDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxUpdateByDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxNhomSanPhamDataGridView);
			this.Name = "NhomSanPhamDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxNhomSanPhamErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNhomSanPhamDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNhomSanPhamBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				NhomSanPhamDataGridViewEventArgs args = new NhomSanPhamDataGridViewEventArgs();
				args.NhomSanPham = _currentNhomSanPham;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class NhomSanPhamDataGridViewEventArgs : System.EventArgs
		{
			private Entities.NhomSanPham	_NhomSanPham;
	
			/// <summary>
			/// the  Entities.NhomSanPham instance.
			/// </summary>
			public Entities.NhomSanPham NhomSanPham
			{
				get { return _NhomSanPham; }
				set { _NhomSanPham = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxNhomSanPhamDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnNhomSanPhamDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxNhomSanPhamDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNhomSanPhamDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNhomSanPhamDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxNhomSanPhamIdDataGridViewColumn":
						e.Value = NhomSanPhamList[e.RowIndex].NhomSanPhamId;
						break;
					case "uxSanPhamIdDataGridViewColumn":
						e.Value = NhomSanPhamList[e.RowIndex].SanPhamId;
						break;
					case "uxLastUpdateDataGridViewColumn":
						e.Value = NhomSanPhamList[e.RowIndex].LastUpdate;
						break;
					case "uxUpdateByDataGridViewColumn":
						e.Value = NhomSanPhamList[e.RowIndex].UpdateBy;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxNhomSanPhamDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNhomSanPhamDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNhomSanPhamDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxNhomSanPhamIdDataGridViewColumn":
						NhomSanPhamList[e.RowIndex].NhomSanPhamId = (System.Guid)e.Value;
						break;
					case "uxSanPhamIdDataGridViewColumn":
						NhomSanPhamList[e.RowIndex].SanPhamId = (System.String)e.Value;
						break;
					case "uxLastUpdateDataGridViewColumn":
						NhomSanPhamList[e.RowIndex].LastUpdate = (System.DateTime)e.Value;
						break;
					case "uxUpdateByDataGridViewColumn":
						NhomSanPhamList[e.RowIndex].UpdateBy = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
